/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.erp.server;

/**
 *
 * @author Administrator
 */
import com.erp.client.DAO.UserBaseInf;
import com.erp.client.systemEntryPoint;
import com.erp.server.DAO.FileDao;
import com.google.gwt.http.client.URL;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class myFiledown extends HttpServlet {

    private int StaticEmpID;// = lls.get(0).getUserId();
    private int StaticDepId;//= lls.get(0).getDepId();
    private String StaticUserName;// = lls.get(0).getUserName();
    private String StaticDepName;//= lls.get(0).getDepName();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        List<UserBaseInf> userbaseinf = new ArrayList<UserBaseInf>();        
        HttpSession session = request.getSession();
        UserBaseInf tempuserbaseinf = new UserBaseInf();
        tempuserbaseinf = (UserBaseInf) session.getAttribute("empuser");
        userbaseinf.add(tempuserbaseinf);

        StaticUserName = userbaseinf.get(0).getUserName();
        StaticDepName = userbaseinf.get(0).getDepName();
        StaticEmpID = userbaseinf.get(0).getUserId();
        StaticDepId = userbaseinf.get(0).getDepId();

        //设置ContentType头字段的值为浏览器无法使用某种方式打开或激活某个程序来处理的类型.
        //可以为application/x-msdownload或者application/octet-stream
        response.setContentType("application/octet-stream;charset=gb2312");
        request.setCharacterEncoding("gb2312");
        response.setCharacterEncoding("gb2312");
        //response.setContentType("application/x-msdownload;charset=gb2312");

        //设置Content-Disposition头的值为"attachment;filename=文件名"
        String name1=request.getParameter("id1");
        int name2=StaticEmpID;
        System.out.print(name2);
        String fileName="";
        try {
            fileName = new FileDao().selectFileID(name1, name2);
        } catch (SQLException ex) {
            Logger.getLogger(myFiledown.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.print("myfiledown.fileName: "+fileName);
        response.addHeader("Content-Disposition", "attachment;filename="+new String( fileName.getBytes("gb2312"), "ISO8859-1" ));
        //从response中获得一个ServletOutputStream
        ServletOutputStream sops = response.getOutputStream();
        //从服务器上获得一个文件,这里用的是下面的web.xml文件.
        String realpath="D:\\upload\\"+ StaticDepName +"\\"+fileName;
        //String s1=request.getSession().getServletContext().getRealPath (realpath);//request.getRequestURI());
        //String s = getServletContext().getRealPath(realpath);//20090629090619-测试员-技术部门-tracert1.txt");
        File file = new File(realpath);
        InputStream ips = new FileInputStream(file);
        byte[] content = new byte[(int) file.length()];
        ips.read(content);
        //将文件内容写入到输出流中发送给客户端
        sops.write(content);
        ips.close();
        sops.close();
    }
}
